﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using YArchitech.BIM;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace YArchitech.HYSpace
{
	public partial class EditSpaceInfo : System.Windows.Forms.Form
	{
		public EditSpaceInfo(Document document, IList<Space> spaceList)
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
			EditSpaceInfo.doc = document;
			this.listSpace = spaceList;
			this.listPara = SpaceParameterOperate.GetParameterName();
			this.tvSpaceName.HideSelection = false;
			this.tvSpaceName.DrawMode = TreeViewDrawMode.OwnerDrawText;
			this.tvSpaceName.DrawNode += this.tvSpaceUse_DrawNode;
			EditSpaceInfo.listTAssign = this.loadParam.GetAllTimeInfo();
			this.SetControlsEvent();
		}

		private void EditSpaceInfo_Load(object sender, EventArgs e)
		{
			this.listInfo = EditSpaceInfo.InitialSpaceInfo(this.listPara, this.listSpace);
			if (this.listInfo == null)
			{
				YJKMessageBox.Show("没有要编辑的空间参数\n建议用空间创建功能之后\n再用空间编辑功能！", MessageBoxButtons.OK);
				base.Dispose();
				return;
			}
			this.InitialTreeViewInfo(this.tvSpaceName, this.listInfo, 0);
			foreach (HYSpaceInfo hyspaceInfo in this.listInfo)
			{
				if (this.tvSpaceName.SelectedNode.Tag.ToString() == hyspaceInfo.Id)
				{
					this.SetUCSpaceParameter(hyspaceInfo, 2);
				}
			}
			this.SetToolTipUcSpace();
		}

		private void SetControlsEvent()
		{
			foreach (object obj in this.ucSpaceParameter1.Controls)
			{
				TextBox textBox = obj as TextBox;
				if (textBox != null)
				{
					textBox.Leave += this.Text_Leave;
				}
				ComboBox comboBox = obj as ComboBox;
				if (comboBox != null)
				{
					comboBox.SelectedIndexChanged += this.Cmb_ValueChanged;
				}
				Button button = obj as Button;
				if (button != null)
				{
					button.Click += this.ShowTimeInfo;
				}
			}
		}

		private void SetToolTipUcSpace()
		{
			this.ucSpaceParameter1.cmbXinFengSJB.MouseHover += this.cmbXinFengSJB_MouseHover;
			this.ucSpaceParameter1.cmbRenYuanSJB.MouseHover += this.cmbXinFengSJB_MouseHover;
			this.ucSpaceParameter1.cmbZhaoMingSJB.MouseHover += this.cmbXinFengSJB_MouseHover;
			this.ucSpaceParameter1.cmbDianDongSBSJB.MouseHover += this.cmbXinFengSJB_MouseHover;
			this.ucSpaceParameter1.cmbDianReSBSJB.MouseHover += this.cmbXinFengSJB_MouseHover;
			this.ucSpaceParameter1.cmbDianZiSBSJB.MouseHover += this.cmbXinFengSJB_MouseHover;
		}

		private void cmbXinFengSJB_MouseHover(object sender, EventArgs e)
		{
			ComboBox comboBox = (ComboBox)sender;
			this.toolTip1.SetToolTip(comboBox, comboBox.SelectedItem.ToString());
		}

		private void btnExit_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		public static IList<HYSpaceInfo> InitialSpaceInfo(IList<string> listPara, IList<Space> listSpace)
		{
			IList<HYSpaceInfo> list = new List<HYSpaceInfo>();
			foreach (Space space in listSpace)
			{
				HYSpaceInfo hyspaceInfo = new HYSpaceInfo();
				hyspaceInfo.Id = space.Id.ToString();
				hyspaceInfo.Name = space.Name;
				hyspaceInfo.Param = new List<SpacePara>();
				foreach (string text in listPara)
				{
					if (!YJKRevitTools.ShareParaBeExist(space, text))
					{
						return null;
					}
					SpacePara spacePara = new SpacePara();
					Parameter parameter = space.GetParameter(text);
					if (parameter.HasValue)
					{
						if (text == "劳动强度(enum)" || text.Contains("时间表"))
						{
							spacePara.Value = parameter.AsString();
						}
						else if (text.Contains("湿度"))
						{
							spacePara.Value = (parameter.AsDouble() * 100.0).ToString();
						}
						else
						{
							spacePara.Value = parameter.AsDouble().ToString();
						}
						spacePara.Key = text;
						hyspaceInfo.Param.Add(spacePara);
					}
				}
				list.Add(hyspaceInfo);
			}
			return list;
		}

		public void InitialTreeViewInfo(TreeView tv, IList<HYSpaceInfo> listSpaceInfo, int selIndex)
		{
			foreach (HYSpaceInfo hyspaceInfo in listSpaceInfo)
			{
				TreeNode treeNode = new TreeNode();
				treeNode.Text = hyspaceInfo.Name;
				treeNode.Tag = hyspaceInfo.Id;
				tv.Nodes.Add(treeNode);
			}
			this.tvSpaceName.SelectedNode = this.tvSpaceName.Nodes[selIndex];
		}

		public void SetUCSpaceParameter(HYSpaceInfo hySpace, int txtNum)
		{
			this.BandCMBDataSource(EditSpaceInfo.listTAssign);
			foreach (object obj in this.ucSpaceParameter1.Controls)
			{
				TextBox textBox = obj as TextBox;
				if (textBox != null)
				{
					foreach (SpacePara spacePara in hySpace.Param)
					{
						if (textBox.Tag.ToString() == spacePara.Key)
						{
							string text;
							if (spacePara.Key.Contains("温度"))
							{
                                text = UnitConvert.CovertFromAPI((DisplayUnitType)57, Convert.ToDouble(spacePara.Value)).ToString();
							}
							else
							{
								text = spacePara.Value;
							}
							if (spacePara.Key == "人员总数" || spacePara.Key == "噪音" || spacePara.Key == "用水量标准")
							{
								textBox.Text = text;
							}
							else
							{
								textBox.Text = StringTools.CutOffEfficacious(text, txtNum);
							}
						}
					}
				}
				ComboBox comboBox = obj as ComboBox;
				if (comboBox != null)
				{
					foreach (SpacePara spacePara2 in hySpace.Param)
					{
						if (comboBox.Tag.ToString() == spacePara2.Key)
						{
							if (spacePara2.Key == "劳动强度(enum)")
							{
								int selectedIndex = (int)Enum.Parse(typeof(LaborIntensity), spacePara2.Value);
								comboBox.SelectedIndex = selectedIndex;
							}
							else
							{
								comboBox.SelectedItem = spacePara2.Value;
							}
						}
					}
				}
			}
		}

		private void BandCMBDataSource(IList<TimeAssign> listTA)
		{
			List<TimeAssign> list = new List<TimeAssign>();
			List<TimeAssign> list2 = new List<TimeAssign>();
			List<TimeAssign> list3 = new List<TimeAssign>();
			List<TimeAssign> list4 = new List<TimeAssign>();
			List<TimeAssign> list5 = new List<TimeAssign>();
			List<TimeAssign> list6 = new List<TimeAssign>();
			this.ucSpaceParameter1.cmbRenYuanSJB.Items.Clear();
			this.ucSpaceParameter1.cmbDianReSBSJB.Items.Clear();
			this.ucSpaceParameter1.cmbDianZiSBSJB.Items.Clear();
			this.ucSpaceParameter1.cmbDianDongSBSJB.Items.Clear();
			this.ucSpaceParameter1.cmbZhaoMingSJB.Items.Clear();
			this.ucSpaceParameter1.cmbXinFengSJB.Items.Clear();
			foreach (TimeAssign timeAssign in listTA)
			{
				string tsParent = timeAssign.TsParent;
				if (!(tsParent == "人员"))
				{
					if (!(tsParent == "新风"))
					{
						if (!(tsParent == "照明"))
						{
							if (!(tsParent == "电动设备"))
							{
								if (!(tsParent == "电子设备"))
								{
									if (tsParent == "电热设备")
									{
										list6.Add(timeAssign);
										this.ucSpaceParameter1.cmbDianReSBSJB.Items.Add(timeAssign.TsName);
									}
								}
								else
								{
									list5.Add(timeAssign);
									this.ucSpaceParameter1.cmbDianZiSBSJB.Items.Add(timeAssign.TsName);
								}
							}
							else
							{
								list4.Add(timeAssign);
								this.ucSpaceParameter1.cmbDianDongSBSJB.Items.Add(timeAssign.TsName);
							}
						}
						else
						{
							list2.Add(timeAssign);
							this.ucSpaceParameter1.cmbZhaoMingSJB.Items.Add(timeAssign.TsName);
						}
					}
					else
					{
						list3.Add(timeAssign);
						this.ucSpaceParameter1.cmbXinFengSJB.Items.Add(timeAssign.TsName);
					}
				}
				else
				{
					list.Add(timeAssign);
					this.ucSpaceParameter1.cmbRenYuanSJB.Items.Add(timeAssign.TsName);
				}
			}
		}

		private void tvSpaceUse_DrawNode(object sender, DrawTreeNodeEventArgs e)
		{
			e.DrawDefault = true;
			if ((e.State & TreeNodeStates.Selected) != (TreeNodeStates)0)
			{
				e.Graphics.FillRectangle(Brushes.DarkBlue, e.Node.Bounds);
				Font font = e.Node.NodeFont;
				if (font == null)
				{
					font = ((TreeView)sender).Font;
				}
				e.Graphics.DrawString(e.Node.Text, font, Brushes.White, Rectangle.Inflate(e.Bounds, 2, 0));
			}
			else
			{
				e.DrawDefault = true;
			}
			if ((e.State & TreeNodeStates.Focused) != (TreeNodeStates)0)
			{
                using (Pen pen = new Pen(System.Drawing.Color.Black))
				{
					pen.DashStyle = DashStyle.Dot;
					Rectangle bounds = e.Node.Bounds;
					bounds.Size = new Size(bounds.Width - 1, bounds.Height - 1);
					e.Graphics.DrawRectangle(pen, bounds);
				}
			}
		}

		private void tvSpaceName_AfterSelect(object sender, TreeViewEventArgs e)
		{
			this.isTreeChange = true;
			foreach (HYSpaceInfo hyspaceInfo in this.listInfo)
			{
				if (this.tvSpaceName.SelectedNode.Tag.ToString() == hyspaceInfo.Id)
				{
					this.SetUCSpaceParameter(hyspaceInfo, 2);
				}
			}
		}

		public void Text_Leave(object sender, EventArgs e)
		{
			if (this.isTreeChange)
			{
				this.isTreeChange = false;
				return;
			}
			foreach (HYSpaceInfo hyspaceInfo in this.listInfo)
			{
				if (!(hyspaceInfo.Id.ToString() != this.tvSpaceName.SelectedNode.Tag.ToString()))
				{
					TextBox textBox = (TextBox)sender;
					if (string.IsNullOrEmpty(textBox.Text))
					{
						break;
					}
					if (textBox.Text == ".")
					{
						textBox.Text = "0";
					}
					foreach (SpacePara spacePara in hyspaceInfo.Param)
					{
						if (textBox.Tag.ToString() == spacePara.Key && spacePara.Key.Contains("温度"))
						{
							spacePara.Value = UnitConvert.CovertToAPI(Convert.ToDouble(textBox.Text), (Autodesk.Revit.DB.DisplayUnitType)57).ToString();
						}
						else if (textBox.Tag.ToString() == spacePara.Key)
						{
							spacePara.Value = textBox.Text;
						}
					}
				}
			}
		}

		private void Cmb_ValueChanged(object sender, EventArgs e)
		{
			if (this.isTreeChange)
			{
				this.isTreeChange = false;
				return;
			}
			foreach (HYSpaceInfo hyspaceInfo in this.listInfo)
			{
				if (!(hyspaceInfo.Id != this.tvSpaceName.SelectedNode.Tag.ToString()))
				{
					ComboBox comboBox = (ComboBox)sender;
					foreach (SpacePara spacePara in hyspaceInfo.Param)
					{
						if (comboBox.Tag.ToString() == spacePara.Key)
						{
							if (spacePara.Key == "劳动强度(enum)")
							{
								spacePara.Value = comboBox.SelectedIndex.ToString();
							}
							else
							{
								spacePara.Value = Convert.ToString(comboBox.SelectedItem);
							}
						}
					}
				}
			}
		}

		private void ShowTimeInfo(object sender, EventArgs e)
		{
			string nodeText = "";
			string name = ((Button)sender).Name;
			if (!(name == "btnDianDongSBSJB"))
			{
				if (!(name == "btnDianReSBSJB"))
				{
					if (!(name == "btnDianZiSBSJB"))
					{
						if (!(name == "btnRenYuanSJB"))
						{
							if (!(name == "btnXinFengSJB"))
							{
								if (name == "btnZhaoMingSJB")
								{
									nodeText = this.ucSpaceParameter1.cmbZhaoMingSJB.SelectedItem.ToString();
								}
							}
							else
							{
								nodeText = this.ucSpaceParameter1.cmbXinFengSJB.SelectedItem.ToString();
							}
						}
						else
						{
							nodeText = this.ucSpaceParameter1.cmbRenYuanSJB.SelectedItem.ToString();
						}
					}
					else
					{
						nodeText = this.ucSpaceParameter1.cmbDianZiSBSJB.SelectedItem.ToString();
					}
				}
				else
				{
					nodeText = this.ucSpaceParameter1.cmbDianReSBSJB.SelectedItem.ToString();
				}
			}
			else
			{
				nodeText = this.ucSpaceParameter1.cmbDianDongSBSJB.SelectedItem.ToString();
			}
			if (new FrmTimeAssignManage(nodeText, EditSpaceInfo.listTAssign).ShowDialog() == DialogResult.OK)
			{
				foreach (HYSpaceInfo hyspaceInfo in this.listInfo)
				{
					if (this.tvSpaceName.SelectedNode.Tag.ToString() == hyspaceInfo.Id)
					{
						this.SetUCSpaceParameter(hyspaceInfo, 2);
					}
				}
			}
		}

		private void btnSure_Click(object sender, EventArgs e)
		{
			double num = Convert.ToDouble(this.ucSpaceParameter1.txtDongJiKTSJWD.Text);
			double num2 = Convert.ToDouble(this.ucSpaceParameter1.txtXiaJiKTSJWD.Text);
			double xjXDSD = Convert.ToDouble(this.ucSpaceParameter1.txtXiaJiKTSJXDSD.Text);
			double djXDSD = Convert.ToDouble(this.ucSpaceParameter1.txtDongJiCNSJXDSD.Text);
			double freshAir = Convert.ToDouble(this.ucSpaceParameter1.txtXinFengGYL.Text);
			double num3 = Convert.ToDouble(this.ucSpaceParameter1.txtDongJiCNSJWD.Text);
			if (num >= 32.0)
			{
				YJKMessageBox.Show("冬季设计温度应该小于32℃");
				this.ucSpaceParameter1.txtDongJiKTSJWD.Select();
				return;
			}
			if (num2 < 13.0 || num2 >= 113.0)
			{
				YJKMessageBox.Show("设计温度应该在13-113间");
				this.ucSpaceParameter1.txtXiaJiKTSJWD.Select();
				return;
			}
			if (num3 >= 32.0)
			{
				YJKMessageBox.Show("冬季设计温度应该小于32℃");
				this.ucSpaceParameter1.txtDongJiCNSJWD.Select();
				return;
			}
			Transaction transaction = new Transaction(EditSpaceInfo.doc, "修改空间参数");
			transaction.Start();
			int num4 = 0;
			foreach (HYSpaceInfo hyspaceInfo in this.listInfo)
			{
				ElementId elementId = new ElementId(Convert.ToInt32(hyspaceInfo.Id));
				Element element = EditSpaceInfo.doc.GetElement(elementId);
				EditSpaceInfo.SetZoneParameter(num2, xjXDSD, djXDSD, freshAir, num3, element);
				this.SetRevitSpaceParam(element as Space, hyspaceInfo);
				IList<bool> list = new List<bool>();
				foreach (SpacePara spacePara in hyspaceInfo.Param)
				{
					Parameter parameter = element.GetParameter(spacePara.Key);
					if (spacePara.Key.Contains("时间表"))
					{
						list.Add(parameter.Set(spacePara.Value));
					}
					else if (spacePara.Key.Contains("湿度"))
					{
						list.Add(parameter.Set(Convert.ToDouble(spacePara.Value) / 100.0));
					}
					else if (spacePara.Key.Contains("温度"))
					{
						list.Add(parameter.Set(Convert.ToDouble(spacePara.Value)));
					}
					else if (spacePara.Key.Equals("劳动强度(enum)"))
					{
						list.Add(parameter.Set(spacePara.Value));
					}
					else
					{
						list.Add(parameter.Set(Convert.ToDouble(spacePara.Value)));
					}
				}
				IEnumerator<bool> enumerator3 = list.GetEnumerator();
				{
					while (enumerator3.MoveNext())
					{
						if (!enumerator3.Current)
						{
							num4++;
						}
					}
				}
			}
			if (num4 == 0)
			{
				YJKMessageBox.Show("修改成功！");
			}
			transaction.Commit();
			base.Close();
		}

		private static void SetZoneParameter(double xjSJWD, double xjXDSD, double djXDSD, double freshAir, double djCNWD, Element elem)
		{
			ZoneInfoManager zoneInfoManager = new ZoneInfoManager(EditSpaceInfo.doc);
			Zone zone = zoneInfoManager.BeExsitZone(xjSJWD, djCNWD, xjXDSD, djXDSD, freshAir);
			Space space = elem as Space;
			SpaceSet spaceSet = new SpaceSet();
			if (zone == null)
			{
				zone = zoneInfoManager.CreatHYZone(EditSpaceInfo.doc, "");
				zoneInfoManager.SetZonePara(zone, xjSJWD, djCNWD, xjXDSD, djXDSD, freshAir);
				spaceSet.Insert(space);
				zone.AddSpaces(spaceSet);
				return;
			}
			zoneInfoManager.SetZonePara(zone, xjSJWD, djCNWD, xjXDSD, djXDSD, freshAir);
			spaceSet.Insert(space);
			if (zone.Id != space.Zone.Id)
			{
				zone.AddSpaces(spaceSet);
			}
		}

		public void SetRevitSpaceParam(Space space, HYSpaceInfo hySpace)
		{
            double num = UnitConvert.CovertFromAPI((DisplayUnitType)12, space.Area);
			foreach (SpacePara spacePara in hySpace.Param)
			{
				string key = spacePara.Key;
				if (!(key == "总冷指标"))
				{
					if (!(key == "总热指标"))
					{
						if (!(key == "新风量"))
						{
							if (!(key == "人员密度"))
							{
								if (key == "人员总数")
								{
									space.OccupancyUnit = 0;
									space.NumberofPeople = UnitConvert.CovertToAPI(Convert.ToDouble(spacePara.Value), (Autodesk.Revit.DB.DisplayUnitType)17);
								}
							}
						}
						else
						{
							space.GetParameter(BuiltInParameter.ROOM_DESIGN_SUPPLY_AIRFLOW_PARAM).Set(UnitConvert.CovertToAPI(Convert.ToDouble(spacePara.Value), (Autodesk.Revit.DB.DisplayUnitType)66));
						}
					}
					else
					{
						Parameter parameter = space.GetParameter(BuiltInParameter.ROOM_DESIGN_HEATING_LOAD_PARAM);
						double num2 = UnitConvert.CovertToAPI(Convert.ToDouble(spacePara.Value), (Autodesk.Revit.DB.DisplayUnitType)40);
						parameter.Set(num2 * num / 1000.0);
					}
				}
				else
				{
					Parameter parameter2 = space.GetParameter(BuiltInParameter.ROOM_DESIGN_COOLING_LOAD_PARAM);
					double num3 = UnitConvert.CovertToAPI(Convert.ToDouble(spacePara.Value), (Autodesk.Revit.DB.DisplayUnitType)40);
					parameter2.Set(num3 * num / 1000.0);
				}
			}
		}

		public static IList<HYSpaceInfo> GetAllHYSpaceInfo(Document doc)
		{
			new List<HYSpaceInfo>();
			IList<string> parameterName = SpaceParameterOperate.GetParameterName();
			IEnumerable<Element> elementByCategory = YJKElementFilter.GetElementByCategory(doc, BuiltInCategory.OST_MEPSpaces);
			IList<Space> list = new List<Space>();
			foreach (Element element in elementByCategory)
			{
				list.Add(element as Space);
			}
			return EditSpaceInfo.InitialSpaceInfo(parameterName, list);
		}

		private static Document doc = null;

		private IList<Space> listSpace = new List<Space>();

		private IList<string> listPara = new List<string>();

		public IList<HYSpaceInfo> listInfo = new List<HYSpaceInfo>();

		public static IList<TimeAssign> listTAssign = new List<TimeAssign>();

		private bool isTreeChange;

		private LoadParamOperate loadParam = new LoadParamOperate();
	}
}
